#include"gui.h"
#include"gui_button.h"
#include"gui_label.h"
#include"gui_container.h"
#include"gui_list.h"
#include<stdio.h>
#include<assert.h>


static int gui_list_create_items(gui_list_t *list,int nums,int width,int height,int item_w,int item_h,int padding)
{
    int x=width/2-item_w/2,y=padding;

    for(int i=0;i<nums;i++)
    {
        gui_spirit_t *button=gui_button_create();
        if(!button)
            return -1;
        gui_spirit_reset_size(button,item_w,item_h);

        button->x=x;
        button->y=y;
        y+=item_h+padding;

        list->buttons[i]=button;

        button->container=gui_container_create(button);
        if(!button->container)
            return -1;

        gui_container_add(GUI_CONTAINER(&list->spirit),button);
    }
    return 0;
}

gui_spirit_t *gui_list_create_no_padding(int nums,int width,int height,int item_w,int item_h)
{
    return gui_list_create(nums,width,height,item_w,item_h,0);
}

gui_spirit_t *gui_list_create(int nums,int width,int height,int item_w,int item_h,int padding)
{
    if(item_w+padding>width)
        item_w=width-padding;

    dprintf("[gui_list] create list w %d h %d num %d item w %d item h %d\n",width,height,nums,item_w,item_h);

    gui_list_t *list=malloc(sizeof(gui_list_t));
    if(!list)
        return NULL;

    gui_spirit_t *spirit=&list->spirit;
    gui_spirit_init(spirit,0,0,width,height);   
    spirit->type=GUI_SPIRIT_TYPE_LIST;
    spirit->container=gui_container_create(spirit);
    if(!spirit->container)
        return NULL;
    gui_surface_t *surface=gui_surface_create(width,height);
    if(!surface)
        return NULL;
    gui_surface_rectfill(surface,0,0,width,height,GUI_GRAY);
    spirit->surface=surface;

    list->list_num=nums;
    list->buttons=malloc(sizeof(gui_button_t*)*nums);
    if(!list->buttons)
        return NULL;
    
    if(item_w==0&&item_h==0)
    {
        item_w=width;
        item_h=height/nums;
        if(item_h<0)
            item_h=8;
    }    

    if( gui_list_create_items(list,nums,width,height,item_w,item_h,padding)<0)
        return NULL;
    
    return spirit;
}

int gui_list_set_item_text(gui_list_t *list,int idx,char *text,int len)
{
    if(idx>=list->list_num)
      return -1;
    gui_button_t **buttons=list->buttons;
    assert(buttons);
    return gui_spirit_set_text(buttons[idx],text);
}

char *gui_list_get_item_text(gui_list_t *list,int idx)
{
    if(idx>=list->list_num)
        return NULL;
    gui_button_t **buttons=list->buttons;
    assert(buttons);
    return buttons[idx]->spirit.text;
}


gui_spirit_t *gui_list_get_item(gui_list_t *list,int idx)
{
    if(idx>=list->list_num)
        return NULL;
    return (gui_spirit_t*)list->buttons[idx];
}

int gui_list_set_disable(gui_list_t *list,int idx)
{
    if(idx>=list->list_num)
      return -1;
    gui_button_t **buttons=list->buttons;
    assert(buttons);
    if(idx>=list->list_num)
        return -1;
    return gui_button_set_disable(buttons[idx]);
}

int gui_list_set_enable(gui_list_t *list,int idx)
{
    if(idx>=list->list_num)
       return -1;
    gui_button_t **buttons=list->buttons;
    assert(buttons);
    if(idx>=list->list_num)
        return -1;
    return gui_button_set_enable(buttons[idx]);
}

int gui_list_set_color(gui_list_t *list,int idx,int alls,gui_color_t color)
{ 
    if(idx>=list->list_num)
       return -1;
    gui_button_t **buttons=list->buttons;
    assert(buttons);

    if(alls)
    {
        for(int i=0;i<list->list_num;i++)
        {
            __gui_list_set_color(buttons[i],color);
        }
    }
    else 
    {
        __gui_list_set_color(buttons[idx],color);
    }
    return 0;
}

int gui_list_mouse_motion(gui_spirit_t *spirit,int x,int y)
{
    return gui_mouse_motion(spirit,x,y);
}

int gui_list_mouse_press(gui_spirit_t *spirit,int btn,int x,int y)
{
    return gui_mouse_btn_down(spirit,btn,x,y);
}

int gui_list_mouse_release(gui_spirit_t *spirit,int btn,int x,int y)
{
    return gui_mouse_btn_up(spirit,btn,x,y);
}

void gui_list_set_pos(gui_spirit_t *spirit,int x,int y)
{
    gui_button_t **buttons=GUI_LIST(spirit)->buttons;
    gui_spirit_t *tmp=NULL;
    for(int i=0;i<GUI_LIST(spirit)->list_num;i++)
    {
        tmp=&buttons[i]->spirit; 
        tmp->x+=x;
        tmp->y+=y;
    }
    spirit->x=x;
    spirit->y=y;
}